package com.gu.auxiliaryToolsClass;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: gusongliao
 * @Description: CountDownLatch 示例 演示6个同学离开教室之后，班长锁门
 * @createDate 2023/05/28 16:08
 */

public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {

        // 定义计数
        CountDownLatch countDownLatch = new CountDownLatch(6);

        // 6个同学离开教室
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 号同学离开了教师");

                // 计数 -1
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }

        // 计数不为0一直等待
        countDownLatch.await();

        System.out.println("班长锁门");


    }

}
